#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <iostream>
#include <extractGraph/extractgraph.h>

using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;



int main()
{
    ExtractGraph extractImg;
    Mat srcImage = extractImg.extractComponet( imread("../../resource/image/circult/image2.jpg") );
    Mat templateImg = extractImg.extractComponet( imread("../../resource/image/base/NPN.jpg") );

    Ptr<SIFT> dectector = SIFT::create();
    vector<KeyPoint> srcKeyPoints, templateKeyPoints;
    dectector->detect(srcImage, srcKeyPoints, Mat());
    dectector->detect(templateImg, templateKeyPoints, Mat());
    //输出特征点个数
    cout << "src keypoints nums:" <<  srcKeyPoints.size() << endl;
    cout << "template keypoints nums" << templateKeyPoints.size() << endl;

    //绘制各图片的关键点图
    Mat srcKeyPointsImg, templateKeyPointsImg;
    drawKeypoints(srcImage, srcKeyPoints, srcKeyPointsImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    drawKeypoints(templateImg, templateKeyPoints, templateKeyPointsImg, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
    imshow("srcKeyPoints", srcKeyPointsImg);
    imshow("templateKeyPoints", templateKeyPointsImg);

    //特征点描述，为特征匹配做准备
    Mat srcDescritpor, templateDescriptor;
    dectector->compute(srcImage, srcKeyPoints, srcDescritpor);
    dectector->compute(templateImg, templateKeyPoints, templateDescriptor);

    //获得匹配特征点，并提取最优配对
    FlannBasedMatcher matcher;
    vector<DMatch> matchPoints;
    matcher.match(templateDescriptor, srcDescritpor, matchPoints, Mat());
    cout <<"total match point" << matchPoints.size() << endl;

    //绘制匹配图
    Mat matchImg;
    drawMatches(templateImg, templateKeyPoints, srcImage, srcKeyPoints, matchPoints, matchImg);
    imshow("match", matchImg);

    waitKey(0);
    return 0;
}
